package cn.chengdu.controller;


import cn.chengdu.entity.People;
import cn.chengdu.entity.Staffinfo;
import cn.chengdu.service.IPeopleService;
import cn.chengdu.service.IStaffinfoService;
import cn.chengdu.util.Sendsms;
import com.alibaba.fastjson.JSON;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import java.text.SimpleDateFormat;
import java.util.Date;

import static org.springframework.boot.Banner.Mode.LOG;

/**
 * <p>
 * 病人表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-11-13
 */

@Controller
@RequestMapping("/people")
public class PeopleController {


    @Autowired
    private IPeopleService ipeopleservice;
    @Autowired
    private IStaffinfoService iStaffinfoService;

    @Autowired
    private RedisTemplate redisTemplate;
    @RequestMapping("/inster")
    public  ModelAndView  insterOne(@RequestParam("name")String name,
                                  @RequestParam("idcard")String id,
                                  @RequestParam("tel")String tel
                                 ) {
        ModelAndView mv=new ModelAndView();
        System.out.println("jajajaj");
        if(ipeopleservice.selectByidCard(id,tel)==false) {
            if (ipeopleservice.insterOne(new People(name, id, tel))) {

                mv.setViewName("redirect:/login.html");

                return mv;
            }
        }
        mv.setViewName("redirect:/binding.html");
        return mv;

    }

//查询是否被注册
    @GetMapping("/findPhoneNumber")
    @ResponseBody
    public String findPhoneNumber(People people){

        People people1=ipeopleservice.login(people);
        if(people1==null){
            return "500";
        }else{
            System.out.println("发送雁阵吗");
            int pcode= Sendsms.sendText(people.getPphone());
            System.out.println("接收的验证码："+pcode);
            ValueOperations<String,Integer> operations=redisTemplate.opsForValue();
            operations.set("pcode",pcode);
          //  new msgController(people.getPphone());
            return "200";
        }

    }
    @GetMapping("/findCode")
    @ResponseBody
    public String findCode(People people,int pcode){
        ValueOperations<String,Object> operations=redisTemplate.opsForValue();
        int code1=(int)(operations.get("pcode"));
        System.out.println("用户输入的验证码."+pcode);
        if(pcode==code1){
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            String nowDate= df.format(new Date());

            int depid=(int)(operations.get("depid"));
            Staffinfo staffinfo=new Staffinfo();
            staffinfo.setDepid(depid);
//            判断存储日期是否是今天日期
            if(operations.get("nowDate").equals(nowDate)){
                System.out.println("等于："+operations.get("nowDate"));

                int i=iStaffinfoService.updateSurnum(staffinfo);
                System.out.println("i的值:"+i);
                if(i==0){
                    return "500";
                }
            }else{

                operations.set("nowDate",nowDate);
                System.out.println("不等于："+operations.get("nowDate"));
                int j=iStaffinfoService.updateSurnum5(staffinfo);
                System.out.println("j的值："+j);
                if(j==0){
                    return "500";
                }
            }
            return "200";
        }else{
            return "500";
        }

    }
//    注册
    @PostMapping("/regis")
    @ResponseBody
    public String regis(People people){
        System.out.println("safa："+people);
        int i=ipeopleservice.register(people);
        if(i==0){
            return "500";
        }
        return "200";
    }
    // 验证码
    @PostMapping("/code")
    @ResponseBody
    public String code(People people){

        return "200";
    }

}
